#include <concepts>
#include <cstdio>

template <typename T>
concept Averageable =
    std::is_default_constructible<T>::value && requires(T a, T b) {
      { a += b } -> std::convertible_to<T>;
      { a / size_t{1} } -> std::convertible_to<T>;
    };

// 一个计算平均值的模板函数
template <Averageable T> T mean(const T *arr, size_t len) {
  T result{};
  for (size_t i{}; i < len; ++i) {
    result += arr[i];
  }
  return result / len;
}

int main(int argc, char **argv) {
  const double arr1[]{1.0, 2.0, 3.0, 4.0, 5.0};
  // const auto result1 = mean<double>(arr1, sizeof(arr1) / sizeof(double));
  const auto result1 = mean(arr1, sizeof(arr1) / sizeof(double));
  const int arr2[]{1, 2, 3, 4, 5};
  // const auto result2 = mean<int>(arr2, sizeof(arr2) / sizeof(int));
  const auto result2 = mean(arr2, sizeof(arr2) / sizeof(int));
  printf("Mean of arr1 is %f\n", result1);
  printf("Mean of arr2 is %d\n", result2);
  const char *arr3[]{"hello", "world"};
  // 这里会报错，因为char *不是一个Averageable类型
  // const auto result3 = mean(arr3, sizeof(arr3) / sizeof(char *));
  return 0;
}
